package com.sentiment.servlet;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.bson.Document;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sentiment.bean.OperationInfo;
import com.sentiment.database.bean.Permission;
import com.sentiment.database.bean.SysInfo;
import com.sentiment.database.dbUtil.DbUtil;
import com.sentiment.tools.Paging;
import com.sentiment.webcollector.net.HttpRequest;
import com.sentiment.webcollector.net.HttpResponse;

/**
 * Servlet implementation class PermissionInfoServlet
 */
@WebServlet("/PermissionInfoServlet")
public class PermissionInfoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		doPost(request, response);
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		
		String methodName = request.getParameter("method");
		try{
			Method method = getClass().getMethod(methodName, 
					HttpServletRequest.class, HttpServletResponse.class);
			method.invoke(this, request, response);
		}catch(Exception e){
			e.printStackTrace();
		}
				
	}

	/**
	 * 获取权限数据
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void GetPermissionsList(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		
		List<Permission> perlist = new ArrayList<>();
		int page  = Integer.parseInt(request.getParameter("pagenum"));
		//连接数据库获取权限信息
		DbUtil dbutil = new DbUtil();
		dbutil.openConnection(Permission.dbName, Permission.collection);		
		List<Document> tlist = dbutil.getPage(page, 10);
		long count = dbutil.collectionGetCount();
		dbutil.closeConnection();			
		Permission per = new Permission();
		for(int i = 0; i < tlist.size(); i++){
			per = new Permission(tlist.get(i));
			perlist.add(per);
		}
		
		
		ObjectMapper mapper = new ObjectMapper();
		String jsonArr = mapper.writeValueAsString(perlist);		
		String jsonStr = "{\"recordsTotal\":"+ count +",\"PersInfo\":" + jsonArr+"}";
		System.out.println(jsonStr);
		
		response.setCharacterEncoding("UTF-8");
		response.getWriter().print(jsonStr);
	}
	
	
	/**
	 * 查询、可输入权限名、权限类型、权限代码、权限说明 进行查询
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void Query(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {	
		request.setCharacterEncoding("UTF-8");
		String permissionname = request.getParameter("permissionname");
		String permissiontype = request.getParameter("permissiontype");
		String permissioncode = request.getParameter("permissioncode");
		String per_detail = request.getParameter("per_detail");
		int page  = Integer.parseInt(request.getParameter("pagenum"));
		
		List<String> keylist = new ArrayList<>();
		if(!permissionname.equals("")){
			keylist.add("permissionname");
		}
		if(!permissiontype.equals("")){
			keylist.add("permissiontype");
		}
		if(!permissioncode.equals("")){
			keylist.add("permissioncode");
		}
		if(!per_detail.equals("")){
			keylist.add("per_detail");
		}
		
		if(keylist.size() != 0){
			Permission per = new Permission(permissionname, permissiontype, permissioncode, per_detail);
			String []keys = keylist.toArray(new String [keylist.size()]);
			
			DbUtil db = new DbUtil();
			db.openConnection(Permission.dbName, Permission.collection);			
			List<Document> tlist = new ArrayList<>();
			tlist = db.queryMany(per.toDocument(), keys);
			db.closeConnection();
			
			String jsonStr = "";
			long count = tlist.size();
			if(count != 0){
				tlist = Paging.paging(tlist, page, 10);
				List<Permission> perlist = new ArrayList<>();
				for (int i = 0; i < tlist.size(); i++) {
					per = new Permission(tlist.get(i));
					perlist.add(per);
				}
				
				ObjectMapper mapper = new ObjectMapper();
				String jsonArr = mapper.writeValueAsString(perlist);
				jsonStr = "{\"recordsTotal\":"+ count +",\"PersInfo\":" + jsonArr+"}";
				
			}else{
				jsonStr = "{\"recordsTotal\":" + count +",\"Info\":" + "\"" + OperationInfo.NoSuchPer + "\"" +"}";
			}
			System.out.println(jsonStr);
			response.setCharacterEncoding("UTF-8");
			response.getWriter().print(jsonStr);
		}
	}
}
